<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/5
 * Time: 16:03
 */

namespace App\Http\Admin\Administration\Models;

use Framework\BaseClass\Http\Model;
use App\Eloquent\Oa\Notice as EloquentOaNotice;
use App\Eloquent\Oa\Contacts as EloquentOaContacts;
use App\Eloquent\Oa\NoticeContacts as EloquentOaNoticeContacts;
use Illuminate\Support\Facades\DB;

class OaNotice extends Model
{


    /**
     * 获取编辑数据
     * @Author sxy
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function getOaNoticeInfo($id)
    {
        $result = EloquentOaNotice::where('id', '=', $id)->first();
        if (is_null($result)) xThrow(ERR_API_RECEIVED_OA_NOTICE_NOT_EXIST);
        $res = EloquentOaNoticeContacts::where('oa_notice_id', '=', $id)->with('contacts_id')->get();
        foreach ($res as $k => $v) {
            if ($v['contacts_id']) {
                $range[] = $v['contacts_id']['name'];
            }
        }
        if(isset($range)) {
            $result['range'] = implode(',', $range);
        }else{
            $result['range'] ='';
        }
        return $result;
    }

    /**
     * 软删除通知公告
     * @Author sxy
     * @param $id
     * @return bool|null|void
     */
    public function delNotice($id)
    {
        $role = EloquentOaNotice::find($id);
        if (!$role) {
            return xThrow(ERR_API_RECEIVED_OA_NOTICE_NOT_EXIST);
        }
        return $role->delete();
    }

    /**
     * 获取发布的通知列表
     * @Author sxy
     * @param $page
     * @param $pageSize
     * @param $searchField
     * @return mixed
     */
    public function getSendNoticeList($page, $pageSize, $searchField)
    {

        $list = EloquentOaNotice::where($searchField)->with('publisherInfo');


        $count = $list->count();
        $list = $list->forPage($page, $pageSize)->get();

        foreach ($list as $k => $v) {

            if (is_null($v->publisherInfo)) {
                $v->publisher = "";
                continue;
            }

            unset($v->publisher_id);
            $v->attachment_url ? $v->attachment_url = "有" : $v->attachment_url = "无";
            $v->publisher = $v->publisherInfo->username;
            $v['release_time'] = date('Y-m-d', $v['release_time']);
            unset($v->publisherInfo);

        }




        $data['list'] = $list;
        $data['pageCurrent'] = $page;
        $data['totalRow'] = $count;
        return $data;
    }

    /**
     * 发送通知公告
     * @Author sxy
     * @param $request
     * @return bool
     */
    public function addSendNotice($request)
    {
        DB::beginTransaction();
        $noticePolicy = new EloquentOaNotice();
        if (is_null($request['release_time'])) {
            $request['release_time'] = time();
        } else {
            $request['release_time'] = strtotime($request['release_time']);
        }
        if (isset($request['attachment_url'])) {
            $request['attachment_url'] = $request['attachment_url'];
        } else {
            $request['attachment_url'] = '';
        }


        $result = $noticePolicy->insertGetId([
            'publisher_id' => $request['publisher_id'],
            'title' => $request['title'],
            'key_word' => $request['key_word'],
            'release_time' => $request['release_time'],
            'attachment_url' => $request['attachment_url'],
            'content' => htmlspecialchars_decode($request['content']),
            'is_check' => 0,
            'deleted_at' => null,
            'created_at' => time(),
            'updated_at' => time(),
            'oa_company_id' => $request['oa_company_id'],
            'main_image_url' => $request['main_image_url'],
        ]);

        if ($result == false) {
            DB::rollBack();
            return false;
        }

        $noticeContacts = new EloquentOaNoticeContacts();
        if(isset($request['address_name'])) {
            foreach ($request['address_name'] as $v) {
                if(!$v) continue;
                $result2 = $noticeContacts->insert([
                    'oa_notice_id' => $result,
                    'oa_contacts_id' => $v,
                ]);
                if ($result2 == false) {
                    DB::rollBack();
                    return false;
                }

            }
        }
        DB::commit();
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改编辑的通知公告
     * @Author sxy
     * @param $request
     * @return bool|void
     */
    public function editOaNotice($request)
    {
        DB::beginTransaction();
        $lawPost = EloquentOaNotice::where('id', $request['id'])->first();
        if (!$lawPost) {
            return xThrow(ERR_API_RECEIVED_OA_NOTICE_NOT_EXIST);
        }
        $input = [
            'title' => $request['title'],
            'release_time' => strtotime($request['release_time']),
            'key_word' => $request['key_word'],
            'content' => htmlspecialchars_decode($request['content']),
            'updated_at' => time(),
        ];
        if (isset($request['attachment_url'])) {
            $input['attachment_url'] = $request['attachment_url'];
        }
        if (isset($request['main_image_url'])) {
            $input['main_image_url'] = $request['main_image_url'];
        }
        $result = $lawPost->update($input);
        if ($result == false) {
            DB::rollBack();
            return false;
        }

        $noticeContacts = new EloquentOaNoticeContacts();
        $del = $noticeContacts->where('oa_notice_id', '=', $request['id'])->delete();

        if ($del == false) {
            DB::rollBack();
            return false;
        }
        if(isset($request['address_name'])) {
            foreach ($request['address_name'] as $v) {
                if (!$v) continue;
                $result2 = $noticeContacts->insert([
                    'oa_notice_id' => $request['id'],
                    'oa_contacts_id' => $v,
                ]);

                if ($result2 == false) {
                    DB::rollBack();
                    return false;
                }

            }
        }
        DB::commit();
        if ($result && $result2 && $del) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取审核通知列表
     * @Author sxy
     * @param $page
     * @param $pageSize
     * @param $searchField
     * @return mixed
     */
    public function getCheckNoticeList($page, $pageSize, $searchField)
    {

        $list = EloquentOaNotice::where($searchField)->with('publisherInfo');


        $count = $list->count();
        $list = $list->forPage($page, $pageSize)->get();
        foreach ($list as $k => $v) {

            if (is_null($v->publisherInfo)) {
                $v->publisher = "";
                continue;
            }

            unset($v->publisher_id);
            $v->attachment_url ? $v->enclosure = "有" : $v->enclosure = "无";
            $v->publisher = $v->publisherInfo->username;
            $v['release_time'] = date('Y-m-d', $v['release_time']);
            unset($v->publisherInfo);

        }



        $data['list'] = $list;
        $data['pageCurrent'] = $page;
        $data['totalRow'] = $count;
        return $data;
    }


    /**
     * *审核通知公告
     * @Author sxy
     * @param $request
     * @return bool|void
     */
    public function CheckNotice($request)
    {
        $lawPost = EloquentOaNotice::where('id', $request['id'])->first();
        if (!$lawPost) {
            return xThrow(ERR_API_RECEIVED_OA_NOTICE_NOT_EXIST);
        }
        $input = [
            'is_check' => $request['is_check']
        ];
        $result = $lawPost->update($input);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * /**
     * 获取搜索发布范围
     * @Author sxy
     */
    public function getReleaseScope()
    {
        $ressult = EloquentOaContacts::select('id', 'name')->get();
        return $ressult;
    }

    /**
     *
     * 获取发布人列表
     * @Author sxy
     */
    public function getPublisher()
    {
        $ressult = EloquentOaContacts::select('id', 'name')->get();
        return $ressult;
    }

    public function getContactsId($id){
        $res= EloquentOaContacts::select('id')->whereIn('oa_department_id',$id)->get();
        if(!$res->isEmpty()) {

            foreach ($res as $v) {
                $ids[] = $v['id'];
            }
            return $ids;
        }else{
            return $ids=[];
        }
    }
}